const { onMsg, request } = require(`${process.root}/util`);
const reg = /(?<method>(GET)|(POST)|(PUT)|(DELETE))\s+?(?<url>https?:\/\/.+)/i;
module.exports = {
  name: 'request',
  main: ({ data }) => {
    onMsg(data, async msg => {
      const res = reg.exec(msg);
      if (!res?.groups) return;
      const method = res.groups.method;
      const url = res.groups.url;
      if (!url || !method) return;
      try {
        const httpRes = await request({ url, method });
        const { data = '' } = httpRes;
        return { message: `request: 请求成功\n${data.length > 100 ? '数据过大' : data}` }
      } catch (e) {
        return { message: 'request: 请求失败' }
      }
    }, { checkAdmin: true })
  }
}
